package cc.admin.modules.graduate.entity.sys;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    private String value;
    private TreeNode parent;
    private List<TreeNode> children;

    public TreeNode(String value) {
        this.value = value;
        this.children = new ArrayList<>();
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void addChild(TreeNode child) {
        child.setParent(this);
        this.children.add(child);
    }

    public TreeNode findChild(String value) {
        for (TreeNode child : children) {
            if (child.getValue().equals(value)) {
                return child;
            }
        }
        return null;
    }

    public void editChild(String oldValue, String newValue) {
        TreeNode child = findChild(oldValue);
        if (child != null) {
            child.setValue(newValue);
        }
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "value='" + value + '\'' +
                ", children=" + children +
                '}';
    }
}